import emailModel from './email';
// 获取jwt用户数据
const getJwtUser = async (ctx: any, jwtService: any) => {
  let token: any = ctx.headers['token'];
  token = await jwtService.verify(token, {
    complete: true,
  });
  return token.payload;
};

const deletObjNull = (data: any) => {
  const res: any = {};
  for (const i in data) {
    if (data[i]) {
      res[i] = data[i];
    }
  }
  return res;
};

const sendEmail = data => {
  // 邮箱验证
  const nodemailer = require('nodemailer');
  return new Promise((resolve: any) => {
    const transporter: any = nodemailer.createTransport({
      // host: "smtp.163.com",
      // port: 465, // SMTP 端口
      service: 'qq',
      auth: {
        //发送者的账户和授权码
        user: '1364857150@qq.com', //账户
        pass: 'fxhyzdczuvfkjfjj', //smtp授权码，到邮箱设置下获取
      },
    });
    //发送邮件
    transporter.sendMail(emailModel(data), (error: any) => {
      if (error) {
        resolve({
          message: '发送失败!',
          code: 200,
        });
        console.log(error);
      }
      resolve({
        message: '发送成功!',
        code: 200,
      });
    });
  });
};

export { getJwtUser, deletObjNull, sendEmail };
